package ru.amse.tsyganov.jumleditor.model;

import java.util.HashSet;
import java.util.Set;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import ru.amse.tsyganov.jumleditor.view.ModelChangeListener;

/**
 * An activity graph is a special case of a state machine 
 * that defines a computational process in terms of the 
 * control-flow and object-flow among its constituent actions.
 * It does not extend the semantics of state machines in 
 * a major way but it does define shorthand forms that are 
 * convenient for modeling control-flow and object-flow in 
 * computational and organizational processes.
 * 
 * @author Dmitry Tsyganov
 *
 */
public class ActivityGraph extends ModelElement {
	
	private final Set<State> contents = new HashSet<State>();
	
	private final Set<ModelChangeListener> modelChangeListeners = 
		new HashSet<ModelChangeListener>();
	
	public ActivityGraph() {
	}
	
	public void addState(State state) {
		if (state == null) {
			throw new IllegalArgumentException();
		}
		state.setActivityGraph(this);
		if (contents.add(state)) {
			fireModelChanged();
		}
	}
	
	public boolean removeState(State state) {
		if (state == null) {
			throw new IllegalArgumentException();
		}
		boolean result = contents.remove(state);
		if (result) {
			fireModelChanged();
		}
		return result;
	}
	
	public void removeAllStates() {
		contents.clear();
		fireModelChanged();
	}

	public Iterable<State> getContents() {
		return contents;
	}
	
	public boolean addModelChangeListener(
			ModelChangeListener modelChangeListener) {
		
		return modelChangeListeners.add(modelChangeListener);
	}
	
	public boolean removeModelChangeListener(
			ModelChangeListener modelChangeListener) {
		
		return modelChangeListeners.remove(modelChangeListener);
	}
	
	public Iterable<ModelChangeListener> getModelChangeListeners() {
		return modelChangeListeners;
	}
	
	public void fireModelChanged() {
		for (ModelChangeListener listener : modelChangeListeners) {
			listener.modelChanged(this);
		}
	}

	public void readFromXml(Node element) {
	}

	public void writeToXml(Element element) {
	}

	public String getClassName() {
		return "ActivityGraph";
	}
}
